package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.service.BlackListService;
import com.tanhua.server.service.QuestionService;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "设置相关接口")
@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private BlackListService blackListService;
    @Autowired
    private UserService userService;

    @ApiOperation("黑名单 - 翻页列表")
    @GetMapping("/blacklist")
    public ResponseEntity blackList(@RequestParam(defaultValue = "1") Long page,
                                    @RequestParam(defaultValue = "10") Long pageSize) {
        //设置一页显示最大条数 防止查询100w条
        pageSize = pageSize > 50 ? 50 : pageSize;
        PageResult<UserInfoVoAge> pageResult = settingsService.blackListPage(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("设置陌生人问题 - 保存")
    @PostMapping("/questions")
    public ResponseEntity saveQuestion(@RequestBody Map<String, String> params) {
        questionService.saveQuestion(params);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("用户通用设置 - 读取")
    @GetMapping("/settings")
    public ResponseEntity querySettings() {
        SettingsVo settingsVo = settingsService.querySettings();
        return ResponseEntity.ok(settingsVo);
    }

    @ApiOperation("通知设置 - 保存")
    @PostMapping("/notifications/setting")
    public ResponseEntity saveSettings(@RequestBody SettingsVo vo) {
        settingsService.saveSetting(vo);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("黑名单 - 移除")
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable Long uid) {
        blackListService.removeBlackList(uid);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("修改手机号- 1 发送短信验证码")
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendCodeForModify();
        return ResponseEntity.ok(null);
    }

    @ApiOperation("修改手机号- 2 校验验证码")
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> params) {
        String code = params.get("verificationCode");
        Boolean verification = settingsService.checkVerificationCode(code);
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setVerification(verification);
        return ResponseEntity.ok(settingsVo);
    }

    @ApiOperation("修改手机号- 3 保存")
    @PostMapping("/phone")
    public ResponseEntity updateMobile(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        settingsService.updateMobile(phone);
        return ResponseEntity.ok(null);
    }
}
